package com.yangrong.thread.chapter8;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.LinkedList;
import java.util.List;
import java.util.Optional;

/**
 * 线程生产者消费者综合实例
 * @author YangRong
 * @date 2020年5月28日
 */
public class CaptureService {

	private final static LinkedList<Control> CONTROLS = new LinkedList<>();
	private final static int MAX_WORKER = 4;

	public static void main(String[] args) {
		List<Thread> worker = new ArrayList<>();
		Arrays.asList("M1", "M2", "M3", "M4", "M5", "M6", "M7", "M8", "M9", "M10").stream()
				.map(CaptureService::createCaptureThread).forEach(t -> {
					t.start();
					worker.add(t);
				});

		worker.stream().forEach(t -> {
			try {
				t.join();
			} catch (InterruptedException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		});

		Optional.of("All of capture work finished").ifPresent(System.out::println);
	}

	private static Thread createCaptureThread(String name) {
		return new Thread(() -> {
			Optional.of("The worker [" + Thread.currentThread().getName() + "] BEGIN capture data.")
					.ifPresent(System.out::println);
			;
			synchronized (CONTROLS) {
				while (CONTROLS.size() > MAX_WORKER) {
					try {
						CONTROLS.wait();
					} catch (InterruptedException e) {
						// TODO Auto-generated catch block
						e.printStackTrace();
					}
				}

				CONTROLS.addLast(new Control());
			}

			Optional.of("The worker [" + Thread.currentThread().getName() + "] is working ...")
					.ifPresent(System.out::println);
			try {
				Thread.sleep(10_000);
			} catch (InterruptedException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}

			synchronized (CONTROLS) {
				Optional.of("The worker [" + Thread.currentThread().getName() + "] END capture data.")
						.ifPresent(System.out::println);
				;
				CONTROLS.removeFirst();
				CONTROLS.notifyAll();
			}
		}, name);
	}

	private static class Control {

	}

}
